<?php

/**
 * 后台文章栏目模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: category.class.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class category extends Model
{

    private $category_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->category_table = '#@__categories';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addClass($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->category_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getCategoryInfo($class_id)
    {
        $rs = $this->db->cacheGetRow("SELECT * FROM $this->category_table WHERE cat_id = $class_id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取资讯栏目
     * return article_class,pages
     */
    public function getCategory($reid, $level)
    {
        global $elo; //定义全局变量 返回值
        $prefix_space = "";
        for ($i = 0; $i < $level; $i++) {
            $prefix_space .= "　"; //第一级就增加一个缩进
        }
        $sqltree = "SELECT * FROM $this->category_table WHERE cat_pid = " . $reid . " ORDER BY cat_order DESC, cat_id ASC";
        $rs = $this->db->getAll($sqltree);
        $fuhao = $level == 0 ? '' : '|-';
        if (is_array($rs)) {
            foreach ($rs as $k => $v) {
                $elo .= "<tr>
                    <td class=\"fenlei td_left\"> $prefix_space $fuhao $v[cat_name]</td>
                    <td class='td_left'>$v[category_nicename]</td>
                    <td class='td_left'>$v[category_description]</td>
                    <td id=\"all_link_order_$v[cat_id]\"><span id=\"order_$v[cat_id]\" onclick=\"edit_order($v[cat_id], $v[cat_order])\" title=\"点击修改内容\" style=\"\" onmousemove=\"this.style.background='#ff0000'\" onmouseout=\"this.style.background='#f6f9fd'\"\">$v[cat_order]</span></td>
                    <td align='center'><a href='index.php?m=admin/article.add&cat_id=$v[cat_id]'>发布文章</a>&nbsp;&nbsp;|<a href='index.php?m=admin/category.add&cat_id=$v[cat_id]'>修改</a>&nbsp;|&nbsp;&nbsp;<a onclick=\"{if(confirm('删除将包括该分类的所有信息，确定删除吗?')){return true;}return false;}\"href=\"index.php?m=admin/category.del&cat_id=$v[cat_id]\">删除</a></td>
                    </tr>";
                //echo "<option value=".$v['id'].$select.">".$prefix_space."|-".$v['typename'].$v[id]."</option><br>";
                $this->getCategory($v['cat_id'], $level + 1); //存在下一级就再次递归调用函数
            }
        }
        return $elo; //最后返回树形菜单
    }

    /**
     * del
     * @param int $class_id
     */
    public function delClass($cat_id)
    {
        $rs = $this->db->execute("DELETE FROM $this->category_table WHERE cat_id = $cat_id");
        return $rs;
    }

    /**
     * 递归获取分类数组
     *
     * @param int $reid			    父级id
     * @param int $level			级别
     * @param int $current_id		当前编辑的分类id
     * @param int $tabel			要查询sql table
     * @param int $where			要查询sql where
     */
    public function tree($reid, $level, $current_id, $table="#@__categories")
    {
        global $el; //定义全局变量 返回值
        $prefix_space = "";
        for ($i = 0; $i < $level; $i++) {
            $prefix_space .= "　"; //第一级就增加一个缩进
        }
        $sqltree = "SELECT cat_id, cat_name FROM " . $table . " WHERE cat_pid = " . $reid . " ORDER BY cat_order DESC, cat_id ASC";
        $rs = $this->db->cacheGetAll($sqltree);
        if (is_array($rs)) {
            foreach ($rs as $k => $v) {
                if ($v['cat_id'] == $current_id) {
                    $select = " selected";
                } else {
                    $select = "";
                }
                $el .= '<option value="' . $v['cat_id'] . '" ' . $select . '>' . $prefix_space . '|-' . $v['cat_name'] . '</option><br>';
                //echo "<option value=".$v['id'].$select.">".$prefix_space."|-".$v['typename'].$v[id]."</option><br>";
                $this->tree($v['cat_id'], $level + 1, $current_id, $table); //存在下一级就再次递归调用函数
            }
        }
        return $el; //最后返回树形菜单
    }

}